package com.hbmcc.heshenghuo.functions.glide

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.annotation.DrawableRes
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget

/**
 * Copyright (c) 2020
 * author: whs
 * created on: 2020/9/17 16:22
 * description: Glide图片加载设置类
 */
class ImgOptions(context: Context?, url: String?,
    @DrawableRes
    res: Int?, any: Any?) : ImgOptionsInterface {
    /**
     * 图片设置类
     */
    private var optionData: OptionData = OptionData()
    private val options = RequestOptions()

    init {
        optionData.context = context
        optionData.url = url
        optionData.res = res
        optionData.any = any
    }

    override fun placeholder(placeholderResId: Int?): ImgOptionsInterface {
        if (placeholderResId != null) options.placeholder(placeholderResId)
        return this
    }

    override fun error(errorResId: Int?): ImgOptionsInterface {
        if (errorResId != null) options.error(errorResId)
        return this
    }

    override fun centerCrop(): ImgOptionsInterface {
        options.centerCrop()
        return this
    }

    override fun fitCenter(): ImgOptionsInterface {
        options.fitCenter()
        return this
    }

    override fun centerInside(): ImgOptionsInterface {
        options.centerInside()
        return this
    }

    override fun transform(transformation: BitmapTransformation?): ImgOptionsInterface {
        if (transformation != null) {
            options.transform(transformation)
        }
        return this
    }

    override fun dontTransform(): ImgOptionsInterface {
        options.dontTransform()
        return this
    }

    override fun dontAnimate(): ImgOptionsInterface {
        options.dontAnimate()
        return this
    }

    override fun diskCacheStrategy(strategy: DiskCacheStrategy): ImgOptionsInterface {
        options.diskCacheStrategy(strategy)
        return this
    }

    override fun override(width: Int, height: Int): ImgOptionsInterface {
        options.override(width, height)
        return this
    }

    override fun autoFit(width: Int): ImgOptionsInterface {
        optionData.autoFit = width
        return this
    }

    override fun into(imageView: ImageView?) {
        if (imageView == null) return
        if (optionData.context == null) return
        if (optionData.url == null && optionData.res == null && optionData.any == null) return

        if (optionData.autoFit != null && optionData.autoFit != 0) {
            into(imageView as View)
            return
        }

        Glide.with(optionData.context!!).load(when {
            optionData.url != null -> optionData.url
            optionData.res != null -> optionData.res
            else -> optionData.any
        }).apply(options).into(imageView)
    }

    override fun into(view: View?) {
        if (view == null) return

        if (optionData.context == null) return
        if (optionData.url == null && optionData.res == null && optionData.any == null) return

        val target = object : SimpleTarget<Bitmap>() {
            override fun onResourceReady(resource: Bitmap, transition: com.bumptech.glide.request.transition.Transition<in Bitmap>?) {
                if (optionData.autoFit != null && optionData.autoFit != 0) {
                    val height: Int = heightFitWidth(optionData.autoFit!!.toFloat(), resource.width.toFloat(), resource.height.toFloat()).toInt()
                    val params: ViewGroup.LayoutParams = view.layoutParams
                    params.height = height
                    params.width = optionData.autoFit!!
                    view.layoutParams = params
                }

                if (view is ImageView) {
                    view.setImageBitmap(resource)
                } else {
                    view.background = BitmapDrawable(optionData.context!!.resources, resource)
                }
            }

            override fun onLoadFailed(errorDrawable: Drawable?) {
                super.onLoadFailed(errorDrawable)
                if (errorDrawable == null) return
                if (optionData.autoFit != null && optionData.autoFit != 0) {
                    val height: Int = heightFitWidth(optionData.autoFit!!.toFloat(), errorDrawable.intrinsicWidth.toFloat(),
                        errorDrawable.intrinsicHeight.toFloat()).toInt()
                    val params: ViewGroup.LayoutParams = view.layoutParams
                    params.height = height
                    params.width = optionData.autoFit!!
                    view.layoutParams = params
                }
                if (view is ImageView) {
                    view.setImageDrawable(errorDrawable)
                } else {
                    view.background = errorDrawable
                }
            }
        }
        Glide.with(optionData.context!!).asBitmap().load(when {
            optionData.url != null -> optionData.url
            optionData.res != null -> optionData.res
            else -> optionData.any
        }).apply(options).into(target)
    }

    override fun get(width: Int, height: Int): Bitmap? {
        if (optionData.context == null) return Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8)
        if (optionData.url == null && optionData.res == null && optionData.any == null) return Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8)
        if (optionData.url == "" && optionData.res == 0 && optionData.any == null) return Bitmap.createBitmap(1, 1, Bitmap.Config.ALPHA_8)


        return Glide.with(optionData.context!!).asBitmap().load(when {
            optionData.url != null -> optionData.url
            optionData.res != null -> optionData.res
            else -> optionData.any
        }).apply(options).into(width, height).get()
    }

    override fun intoBitmap(bitmapInterface: ImgOptionsInterface.BitmapInterface) {
        if (optionData.context == null) return
        if (optionData.url == null && optionData.res == null && optionData.any == null) return

        val target = object : SimpleTarget<Bitmap>() {
            override fun onResourceReady(resource: Bitmap, transition: com.bumptech.glide.request.transition.Transition<in Bitmap>?) {
                bitmapInterface.onSuccess(resource)
            }

            override fun onLoadFailed(errorDrawable: Drawable?) {
                super.onLoadFailed(errorDrawable)
                bitmapInterface.onError(errorDrawable)
            }
        }
        Glide.with(optionData.context!!).asBitmap().load(when {
            optionData.url != null -> optionData.url
            optionData.res != null -> optionData.res
            else -> optionData.any
        }).apply(options).into(target)
    }

    /*
     *  宽度固定，获得等比例高度
     * */
    private fun heightFitWidth(w: Float, referW: Float, referH: Float): Float {
        return w / referW * referH
    }


}